package com.ezops.controller;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ezops.dao.facade.IClientDataTransferFacade;
import com.ezops.dao.facade.RuleManagementFacade;
import com.ezops.dto.EzopsFieldUIModel;
import com.ezops.dto.MappingModel;
import com.ezops.dto.ValidationRule;
import com.ezops.models.ClientEzopsFieldMappingModel;
import com.ezops.models.ClientFields;
import com.ezops.models.EzopsFields;

@Controller
@RequestMapping("/mapping")
public class FieldMappingController {

	protected static Logger logger = LoggerFactory.getLogger("controller");

	@Autowired
	private IClientDataTransferFacade facade;

	@Autowired
	RuleManagementFacade ruleFacade;

	@RequestMapping(value = "/clientfieldslookup", method = RequestMethod.GET)
	public @ResponseBody
	List<ClientFields> getClientFields(
			@RequestParam(value = "clientid", required = false, defaultValue = "1") String clientId,
			@RequestParam(value = "assetclass", required = false, defaultValue = "1") String assetClass) {
		return facade.getAllClientFields(clientId, assetClass);
	}

	@RequestMapping(value = "/ezopsfieldslookup", method = RequestMethod.GET)
	public @ResponseBody
	List<EzopsFields> getEzopsFieldsLookUp() {
		List<EzopsFields> ezopsFields = facade.getAllEzopsFields();
		

		return ezopsFields;
	}

	@RequestMapping(value = "/fieldmappings", method = RequestMethod.GET)
	public @ResponseBody
	List<EzopsFieldUIModel> getMappings(
			@RequestParam(value = "clientid", required = false, defaultValue = "1") String clientId,
			@RequestParam(value = "assetclass", required = false, defaultValue = "1") String assetClass) {
		List<ClientEzopsFieldMappingModel> mapping = facade.getAllMapping(
				clientId, assetClass);

		List<EzopsFieldUIModel> modelToReturn = new ArrayList<>();
		List<ValidationRule> rules = ruleFacade.getAllValidationRules();
		for (ClientEzopsFieldMappingModel model : mapping) {
			EzopsFieldUIModel mappingMode = new EzopsFieldUIModel();
			mappingMode.setClientFieldId(Integer.valueOf(model.getClientField()
					.getId()));
			mappingMode
					.setEzopsFieldId(Integer.valueOf(model.getEzopsField().getId()));
			mappingMode.setFieldName(model.getClientField()
					.getFieldName());
			mappingMode.setEzopsFieldName(model.getEzopsField()
					.getEzopsFieldName());

			for (ValidationRule rule : rules) {
				if (rule.getEzopsFieldName().equalsIgnoreCase(
						mappingMode.getEzopsFieldName())) {
					mappingMode.setMandatory(rule.getMandatoryName());
					mappingMode.setType(rule.getType());
					mappingMode.setAssetClass(rule.getAssetName());
					mappingMode.setDblookup(rule.getDbLookupName());
					mappingMode.setRange(rule.getRange());
					mappingMode.setRefColumn(rule.getRefColumnName());

				}
			}

			modelToReturn.add(mappingMode);
		}

		return modelToReturn;
	}
	
	/**
	 * Function to save client-EZOPS field mappings
	 * @param fieldMappingJson
	 * @return
	 */
	@RequestMapping(value = "/savefieldmappings", method = RequestMethod.POST , consumes="application/json")
	public @ResponseBody boolean saveFieldMappings(@RequestBody List<MappingModel> fieldMappingJson) {
		List<ClientEzopsFieldMappingModel> models = new ArrayList<>();
		for(MappingModel map : fieldMappingJson){
			models.add(map.adaptTo());
		}
		facade.saveFieldMappings(models);
		return true;
	}
	

}
